﻿using Scrat.Core.Pojo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Scrat.Core.Common
{
    /// <summary>
    /// 用于格式化ListBox显示消息格式
    /// </summary>
    public class FormatDatagram
    {

        /// <summary>
        /// 格式化消息
        /// </summary>
        /// <param name="container">显示容器ListBox</param>
        /// <param name="message">消息</param>
        /// <param name="identity">身份: 教师, 学生, 其它App</param>
        /// <param name="ipAddr">发送方的IP地址</param>
        public static string[] FormatMessage(Datagram datagram)
        {
            string[] result = new string[2];

            string currentDateTime = DateTime.Now.ToString("HH:mm ss");
            
            string ipAddrTemp = datagram.RemoteIPAddr.ToString();

            /*string ipAddr = ipAddrTemp.Length > 7 ?
                ipAddrTemp.Substring(ipAddrTemp.LastIndexOf(".")) :
                "unknow";*/
            string ipAddr = ipAddrTemp.Length > 7 ?
                ipAddrTemp :
                "unknown";

            if (datagram.DataGramType == DataGramType.Message)
            {
                byte[] msgByte = (byte[])datagram.Data;
                result[1] = Encoding.UTF8.GetString(msgByte);

                switch (datagram.Identity)
                {
                    case Identity.Student:
                        result[0] = "[" + currentDateTime + "] Student{" + ipAddr + "}#";
                        break;
                    case Identity.Teacher:
                        result[0] = "[" + currentDateTime + "] Teacher{" + ipAddr + "}#";
                        break;
                    case Identity.MySelf:
                        result[0] = "[" + currentDateTime + "] Me{" + ipAddr + "}#";
                        break;
                    case Identity.Unknown:
                        result[0] = "[" + currentDateTime + "] Unknown{" + ipAddr + "}#";
                        break;
                    default:
                        Console.WriteLine("FormatStrToMessage: line 43, the FormatMessage() is error!");
                        break;

                }
            }
            else if (datagram.DataGramType == DataGramType.OuterData)
            {
                byte[] msgByte = (byte[])datagram.Data;
                result[1] = Encoding.UTF8.GetString(msgByte);

                result[0] = "[" + currentDateTime + "] Unknown{" + ipAddr + "}#"; 
            }
            else
            {
                Console.WriteLine("FormatStrToMessage: line 53, the FormatMessage() is error!");
            }

            return result;
        }


        /// <summary>
        /// 格式化上线消息格式
        /// </summary>
        /// <param name="datagram"></param>
        /// <returns></returns>
        public static string[] FormatOnlineOrDownlineDataGram(Datagram datagram)
        {
            string[] result = new string[3];
            string currentDateTime = DateTime.Now.ToString("HH:mm ss");

            if (datagram.DataGramType == DataGramType.Online || datagram.DataGramType == DataGramType.Downline)
            {
                result[0] = datagram.RemoteIPAddr.ToString();
                result[1] = datagram.RemotePort.ToString();
                result[2] = currentDateTime;
            }
            return result;
        }
    }
}
